<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Validator;
use Hash;
use Auth;
class PasswordRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */


//    自定义验证规则验证旧密码
    public function guize(){
//        第一个填你自定义的规则
        Validator::extend('jiumima',
            function ($attribute, $value, $parameters, $validator) {
//            return $value == Hash::check($value,Auth::user()->password);
//            第一个参数为你要验证的字段，也就是你的name名
//            dd($attribute);
             return $value == Hash::check($value,Auth::user()->password);
        });
    }





    public function rules()
    {
        $this->guize();
        return [
//            这里边填写验证规则,返回的是一个数组
            "name"=>"required",
//            nullable意思是允许为空
            "oldpassword"=>"nullable|jiumima",
            "password"=>"nullable|between:6,20|confirmed",
            "password_confirmation"=>"nullable|between:6,20",
        ];
    }


//    自定义验证完毕，下一步报错显示中文
    public function messages()
    {
        return [
            "oldpassword.jiumima" => "旧密码输入有误"
        ];
    }
//    验证完毕
}
